<?php
/**
 * 
 * 角色信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:lj
 * Date:2016-03-09
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/

class Role extends CActiveRecord {
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{role}}';
	}
	
	
	/**
	 * 查询
	 */
	public function queryRole($role_name,$role_type){
		$cmodel = new CCCModel();	
		$sql = "SELECT * FROM {{role}} WHERE active=1 ";
		if($role_name!=null&&$role_name!="")
		{
			$sql.= " AND role_name like '%".$role_name."%'";
		}
		if($role_type!=null&&$role_type!="")
		{
			$sql.= " AND role_type =".$role_type;
		}
		
		$result = $cmodel->sel($sql);
		return $result;
	}
	
	/**
	 *根据role_id获取角色信息
	 */
	public function getRoleInfoByRoleId($role_id){
		$cmodel = new CCCModel();
		$sql = "SELECT * FROM {{Role}} WHERE role_id=$role_id";
		$result = $cmodel->sel($sql);
		return $result;
	}
	/**
	 * @author xjh
	 * 修改角色信息
	 */
	public function updateRoleByRoleId($role_id){
		$cmodel = new CCCModel();
		$sql = "UPDATE {{Role}} set role_name='".$_POST['role_name']."',role_type='".$_POST['role_type']."',role_description='".$_POST['role_description']."'  WHERE role_id=$role_id";
		$result = $cmodel -> upd($sql);
		return $result;
	}
	
	/**
	 * @author xjh
	 * 删除角色信息
	 */
	public function DeleteRoleByRoleId($role_id){
		$cmodel = new CCCModel();
		$sql = "UPDATE {{Role}} set active = 0  WHERE role_id=$role_id";
		$result = $cmodel -> upd($sql);
		return $result;
	}
	
	/**
	 * 新增
	 */
	public function addRole($role_name,$role_type,$role_description){
		$cmodel = new CCCModel();
		$sql = "INSERT INTO {{role}}(`role_name`,`role_type`,`role_description`) VALUES ('".$role_name."','".$role_type."','".$role_description."')";
		$result = $cmodel->upd($sql);
		return $result;
	}
	/**
	 * 权限
	 */

	public function queryRolePower($role_id){
		$cmodel = new CCCModel();	
		$sql = "select  b.pages_id,b.pages_name,(select a.pages_name from {{pages}} a where a.pages_id = b.pages_parent) as parent_name,
		(select c.role_access_id from {{role_access}} c where b.pages_id = c.pages_id and c.active = 1 and c.role_id = ".$role_id.") as role_access_id
		 from {{pages}} b where b.active = 1 and pages_parent <> 0 order by pages_parent";		
		$result = $cmodel->sel($sql);
		return $result;
	}
	
	/**
	 * 更新权限
	 */

	public function queryUpdatePower($role_id,$power_arry){	
		$cmodel = new CCCModel();	
		$sql = "update {{role_access}} set active = 0 where role_id=".$role_id;
		$cmodel->upd($sql);
		foreach ($power_arry as $k=>$v){ 			
			$sql = "select * from {{role_access}} where role_id=".$role_id." and pages_id=".$v;
			$result = $cmodel->sel($sql);		
			if(!empty($result)){
			$sql = "update {{role_access}} set active = 1 where role_id=".$role_id." and pages_id=".$v;
			$result = $cmodel->upd($sql);			
			}else {
			$sql = "INSERT INTO {{role_access}}(`role_id`,`pages_id`) VALUES ('".$role_id."','".$v."')";
			$result = $cmodel->upd($sql);
			}
		} 			
	
		return $result;
	}
}

	
?>